<template>
  <div>
    <div style="margin-bottom: 30px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><span style="font-size: 20px">首页</span></el-breadcrumb-item>
        <el-breadcrumb-item><span style="font-size: 20px">人员管理</span></el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="headleAdd">管理班级<i class="el-icon-circle-plus-outline"></i> </el-button>
      <h1></h1>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
	  <el-table-column prop="id" label="id" width="200"></el-table-column>
      <el-table-column prop="sno" label="学号" width="200">
      </el-table-column>
      <el-table-column prop="snoName" label="学生姓名" width="150">
      </el-table-column>
      <el-table-column prop="classesName" label="班级名字" width="150"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
			<el-popconfirm
			    class="ml-5"
			    confirm-button-text='确定'
			    cancel-button-text='我再想想'
			    icon="el-icon-info"
			    icon-color="red"
			    title="确定删除吗？"
			    @confirm="del(scope.row.id)"
			>
            <el-button type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>
            <!--注意这里在后台传了个id-->
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="padding: 10px 0"><!--分页查询-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"><!--分页查询数据绑定-->
        <!--此处的pageNum和pageSize由前端传递,total由后台返回给前端-->
        <!--total记得从后端传一下-->
        <!--在页面中pageNum是翻页,pageSize是左边的 *条每页,total是左边的 共*条-->
      </el-pagination>
    </div>

    <el-dialog title="添加学生到本班" :visible.sync="dialogFormVisible" width="80%">
      <div>
        <div style="margin: 10px 0">
          <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search"
                    v-model="username"></el-input><!--此处的username需要后端传递-->
          <el-button style="margin-left: 5px" type="primary" @click="loadUser()">搜索</el-button>
          <h1></h1>
          <template>
            <el-select v-model="classesId" placeholder="请选择要添加到的班级">
              <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.value"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
		  <div style="padding: 10px 0"><!--分页查询-->
		    <el-pagination
		        @size-change="handleUserSizeChange"
		        @current-change="handleUserCurrentChange"
		        :current-page="pageUserNum"
		        :page-sizes="[2, 5, 10, 20]"
		        :page-size="pageUserSize"
		        layout="total, sizes, prev, pager, next, jumper"
		        :total="totalUser"><!--分页查询数据绑定-->
		      <!--此处的pageNum和pageSize由前端传递,total由后台返回给前端-->
		      <!--total记得从后端传一下-->
		      <!--在页面中pageNum是翻页,pageSize是左边的 *条每页,total是左边的 共*条-->
		    </el-pagination>
		  </div>
        </div>
      <el-table :data="userList" border stripe :header-cell-class-name="headerBg"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="codeName" label="学号" width="200">
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="150">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope"><!--此处的slot-scope已被废弃,但暂没找到替代-->
            <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="确定添加到本班级吗？"
                @confirm="addclass(scope.row.codeName)"
            >
              <el-button type="danger" slot="reference">将学生添加到本班级<i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogFormVisible = false">完成</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
	import {classes_list} from '../axios/api/classesApi.js'
	import {user_page} from '../axios/api/userApi.js'
	import {studentClasses_add,studentClasses_delete,studentClasses_page} from '../axios/api/studentClassesApi.js'
export default {
  name: "StudentClasses",
  data(){

    return{
      tableData:[],
	  userList:[],
      total:0,
      pageNum:1,
      pageSize:10,
	  totalUser:0,
	  pageUserNum:1,
	  pageUserSize:10,
      //username为搜索框的变量,记得绑定
      username:"",
      form:{},
	  user:{},
      dialogFormVisible:false,
      multipleSelection: [],
      headerBg:'headerBg',
      options: [],
      classesId: undefined
    }
  },
  created() {
	  this.user = JSON.parse(localStorage.getItem('user'))
    this.load()
	this.loadUser()
	classes_list(this.user.codeName).then(res=>{
		this.options = res.data.list
	})
  },
  methods:{
    //连接数据库,我在其他需要连接的地方统一调用load()
    //记得改你自己的地址,我的vue的默认端口是8080
    //我此处的连接用了axios，在src/utils文件中的request.js，若不想使用或报错，建议删了我的用自己的方法
    //我把地址放在了request.js开头部分的baseURL中了，原本url为http://localhost:9090/user/page?pageNum=
    load(){
		const data = {
			pageNum: this.pageNum,
			pageSize: this.pageSize
		}
		studentClasses_page(this.user.codeName, data).then(res=>{
			this.tableData = res.data.records
			this.total = res.data.total
		})
    },
	loadUser(){
		const data = {
			pageNum: this.pageUserNum,
			pageSize: this.pageUserSize,
			status: 1,
			name: this.username
		}
		user_page(data).then(res=>{
			this.userList = res.data.records
			this.totalUser = res.data.total
			this.username = ""
		})
	},
    //新增功能,编辑和新增可以用同一个接口
    headleAdd(){
      this.dialogFormVisible = true //打开弹窗
      this.form = {}
    },

    //编辑功能,row是被传入的值,编辑和新增可以用同一个接口,注意后台改一下
    //this.form = Object.assign({},row),将row拷贝到空对象中，可以解决没点确定但数据已经改变的问题
    handleEdit(row){
      this.form = row  //将编辑的值赋予
      this.dialogFormVisible = true //打开弹窗
    },

    //删除功能,这里后台传了个id,地址处理与上面用了相同的手法
    del(id){
		console.log(id)
      studentClasses_delete(id).then(res=>{
		  this.load()
	  })
    },

    classadd(classid){

    },

    addclass(sno){
		const data = {
			sno: sno,
			classesId: this.classesId
		}
		studentClasses_add(data).then(res=>{
			this.load()
		})
    },

    //多选按钮
    handleSelectionChange(val){
      this.multipleSelection = val
    },

    //为pageSize与pageNum设置连接
    handleSizeChange(pageSize){
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum){
      this.pageNum = pageNum
      this.load()
    },
	handleUserSizeChange(pageSize){
	  this.pageUserSize = pageSize
	  this.loadUser()
	},
	handleUserCurrentChange(pageNum){
	  this.pageUserNum = pageNum
	  this.loadUser()
	}
  }
}
</script>

<style>
.headerBg{
  background: #eee!important;
}
</style>
